
package org.dyndns.cooland.updater.dialogs;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.dyndns.cooland.updater.db.OUTStore;
import org.dyndns.cooland.updater.db.dao.SitesDAO;
import org.dyndns.cooland.updater.db.model.Sites;

public class ManageSitesDlg extends JDialog {
	
	private static final long	serialVersionUID	= -7311615116750699842L;
	private final JPanel		contentPanel		= new JPanel();
	private JTextField			txtFilterText;
	private JList				listSites;
	private JButton				btnDisableEnable;
	SitesDAO					dao					= new SitesDAO();
	DefaultListModel			model				= new DefaultListModel();
	
	/**
	 * Create the dialog.
	 */
	public ManageSitesDlg (OUTStore store) {

		setModalityType(ModalityType.APPLICATION_MODAL);
		setModal(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setTitle("Available Software Sites");
		setBounds(100, 100, 681, 517);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			txtFilterText = new JTextField();
			txtFilterText.setEnabled(false);
			txtFilterText.setText("type filter text");
			txtFilterText.setColumns(10);
		}
		
		listSites = new JList(model);
		listSites.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listSites.setDragEnabled(true);
		listSites.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		listSites.setCellRenderer(new SitesListCellRenderer());
		
		listSites.addListSelectionListener(new ListSelectionListener() {
			
			public void valueChanged(ListSelectionEvent e) {

				if (!e.getValueIsAdjusting()){
					Sites bean = (Sites) listSites.getSelectedValue();
					if (bean != null){
						int enabled = bean.getEnabled();
						if (enabled == 0){
							btnDisableEnable.setText("Enable");
						}
						if (enabled == 1){
							btnDisableEnable.setText("Disable");
						}
					}
				}
			}
		});
		
		JButton btnAddButton = new JButton("Add ...");
		btnAddButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {

				AddSiteDlg dlg = new AddSiteDlg(null);
				dlg.setVisible(true);
				updateModel();
			}
		});
		
		JButton btnEditButton = new JButton("Edit ...");
		btnEditButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {

				AddSiteDlg dlg = new AddSiteDlg((Sites) listSites
						.getSelectedValue());
				dlg.setVisible(true);
				updateModel();
			}
		});
		
		JButton btnRemoveButton = new JButton("Remove");
		btnRemoveButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {

				SitesDAO dao = new SitesDAO();
				Sites fordel = (Sites) listSites.getSelectedValue();
				dao.delete(fordel.getId());
				updateModel();
			}
		});
		
		JButton btnTestConnection = new JButton("Test connection");
		btnTestConnection.setEnabled(false);
		
		btnDisableEnable = new JButton("Disable");
		btnDisableEnable.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {

				Sites bean = (Sites) listSites.getSelectedValue();
				int enabled = bean.getEnabled();
				switch (enabled) {
					case 0:
						bean.setEnabled(1);
						break;
					case 1:
						bean.setEnabled(0);
						break;
					default:
						break;
				}
				
				dao.update(bean);
				updateModel();
			}
		});
		
		JButton btnImport = new JButton("Import ...");
		btnImport.setEnabled(false);
		
		JButton btnExport = new JButton("Export ...");
		btnExport.setEnabled(false);
		
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel
				.setHorizontalGroup(gl_contentPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPanel
										.createSequentialGroup()
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.LEADING,
																false)
														.addComponent(
																txtFilterText,
																GroupLayout.PREFERRED_SIZE,
																651,
																GroupLayout.PREFERRED_SIZE)
														.addGroup(
																Alignment.TRAILING,
																gl_contentPanel
																		.createSequentialGroup()
																		.addComponent(
																				listSites,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addGap(
																				18)
																		.addGroup(
																				gl_contentPanel
																						.createParallelGroup(
																								Alignment.TRAILING,
																								false)
																						.addComponent(
																								btnTestConnection,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								btnAddButton,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								btnEditButton,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								btnRemoveButton,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								btnDisableEnable,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								btnImport,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								btnExport,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE))))
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		gl_contentPanel
				.setVerticalGroup(gl_contentPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPanel
										.createSequentialGroup()
										.addComponent(txtFilterText,
												GroupLayout.PREFERRED_SIZE, 20,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_contentPanel
																		.createSequentialGroup()
																		.addComponent(
																				btnAddButton)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				btnEditButton)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				btnRemoveButton)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				btnTestConnection)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				btnDisableEnable)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				btnImport)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				btnExport))
														.addComponent(
																listSites,
																GroupLayout.DEFAULT_SIZE,
																405,
																Short.MAX_VALUE))
										.addContainerGap()));
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent e) {

						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent e) {

						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		// /////////////////////////////////////////////////////////////////////////////
		updateModel();
	}
	
	public void updateModel() {

		model.clear();
		List<Sites> list = dao.selectAll();
		Iterator<Sites> it = list.listIterator();
		while (it.hasNext()){
			Sites site = it.next();
			model.addElement(site);
		}
	}
}

class SitesListCellRenderer implements ListCellRenderer {
	
	protected DefaultListCellRenderer	defaultRenderer	= new DefaultListCellRenderer();
	
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {

		JLabel renderer = (JLabel) defaultRenderer
				.getListCellRendererComponent(list, value, index, isSelected,
						cellHasFocus);
		Sites bean = (Sites) value;
		if (bean != null){
			switch (bean.getEnabled()) {
				case 0:
					renderer.setEnabled(false);
					break;
				case 1:
					renderer.setEnabled(true);
					break;
				default:
					break;
			}
			
		}
		return renderer;
	}
}
